<template>
  <div>
    <div class="my-tree">
      <!-- 权限内容  原先defaultProps:{children:'children',labbel:lable}下面就是结构label赋值-->
      <el-tree ref="treeRef" node-key="id" :data="list" check-strictly show-checkbox :props="{label:'name'}" @node-click="handleNodeClick" />
    </div>
    <div>
      <!-- 按钮 -->
      <el-button type="primary" @click="oneSubm">确认</el-button>
      <el-button @click="concen">取消</el-button>
    </div>
  </div>
</template>
<script>
import { AdddataPermissions } from '@/api/permissions'
import { transformListToTree2 } from '@/utils/index'
import { assignermissionseTting, getassignerseTting } from '@/api/settings'
export default {
  name: '',
  props: {
    currentId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.Adddata()
  },
  methods: {
    // 给角色分配权限请求
    async assigneTting() {
      try {
        const res = await assignermissionseTting({
          id: this.currentId,
          permIds: this.$refs.treeRef.getCheckedKeys()
        })
        console.log('assignermissionseTting: ', res)
        if (res.success) {
          this.concen()
        }
      } catch (err) {
        console.log('assignermissionseTting: ', err)
      }
    },
    // 根据ID获取角色详情回显
    async getassigner() {
      try {
        const res = await getassignerseTting(this.currentId)
        console.log('88888888888888888888888 ', res)
        if (res.success) {
          this.$refs.treeRef.setCheckedKeys(res.data?.permIds || [])
        }
      } catch (err) {
        console.log('getassignerseTting: ', err)
      }
    },
    // 确认按钮
    oneSubm() {
      this.assigneTting()

      console.log(this.$refs.treeRef.getCheckedNodes())
      console.log(this.$refs.treeRef.getCheckedKeys())
    },
    // 取消按钮
    concen() {
      this.$emit('close')
    },
    // 获取全部权限点
    async Adddata() {
      try {
        const res = await AdddataPermissions()
        if (res.success) {
          console.log('AdddataPermissions: ', res)
          // 前面pid ''是一级菜单 现在接口有变'0'是一级
          this.list = transformListToTree2(res.data, '0')
          this.getassigner()
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('权限接口有误', err)
      }
    },
    handleNodeClick() {}
  }
}
</script>
